for i in range(10):
num = int(input("Enter an integer: "))
num0, num1, ..., num9?
What if we need to read a million values for the statistical analysis?
howmany = ???(input("Enther number of integers: ")) # int() converts a string to an integer.
for i in ???(howmany):
num = ???(input("Enter an integer: "))
howmany-many integers and save them into one variable.howmany the user will input.
What do we have to do?
numbers = [10, -2, 3, 99, 0] # Comma separated values with in [ and ] names = ["John", "Ruth", "Tom"] record = ["CS", 4, 3.9, True] print(record)
numbers = [10, -2, 3, 99, 0] # The index of 10 is 0, and the index of -2 is 1, ... print(numbers[2]) # the value of index 2 in numbers. print(numbers[0]) print(["CS", 4, 3.9, True][1]) # possible?
numbers[0], ..., numbers[4] are individual variables, and
they are called elements.
numbers = [10, -2, 3, 99, 0] print(numbers[-1]) # ??? print(numbers[5]) # IndexError: list index out of range ["CS", 4, 3.9, True][10] # IndexError: list index out of rangeAnything strange with
print(numbers[-1])?
numbers = [10, -2, 3, 99, 0] print(numbers[1]) # print(numbers[0]) # print(numbers[-1]) # print(numbers[-2]) # print(numbers[-3]) # print(numbers[-4]) # print(numbers[-5]) # print(numbers[-6]) # IndexError: list index out of range
numbers = [10, -2, 3, 99, 0] numbers[2] = -8 print(numbers)
numbers = [] # An empty list numbers = numbers + [10] # +: list concatenation operator numbers = numbers + [-2] numbers = numbers + [3] + [99, 0] print(numbers)
numbers = [10, -2, 3, 99, 0] names = ["John", "Ruth", "Tom"] test = numbers + names print(test)
numbers = [10, -2, 3, 99, 0] del numbers[1] # del statement: Deletes a variable, the specifed element in this case del(number[1]) # del() function print(numbers)
numbers = [10, -2, 3, 99, 0]
result = 3 in numbers # in: list membership operator
print(result)
result = 3 not in numbers # not in: list membership operator
print(result)
result = '99' in numbers # The string '99' will be converted to an integer 99 first, and then in operator.
print(result)
numbers = []
for i in range(1000):
numbers = numbers + [int(input('Enter integer: '))]
if -1 in numbers:
break;
print(numbers)
numbers = [10, -2, 3, 99, 0]
for i in numbers:
print(i) # Prints elements, not indexes
numbers = [10, -2, 3, 99, 0]
count = ???(numbers) # len(): the function that was used to get the length of a string.
for i in range(count):
print(numbers[i]) # Prints the ith element
numbers = [None, None, None, None, None] # None: a special value meanging that nothing is assigned
for i in ???(5):
numbers[i] = ???(input('Enter an integer: '))
print(numbers)
What if we need to read a million values?
numbers = [] # Empty list
count = int(input('Enter the number of inputs: '))
for i in range(???): # count-many times
value = int(input('Enter an integer: '))
numbers = numbers ??? ??? # +: list concatenation operator
print(numbers)
.append() method.
Note a method is a function belonging to an object.
Let's try the next example.
numbers = [] # Empty list
count = int(input('Enter the number of inputs: '))
for i in range(count):
value = int(input('Enter an integer: '))
numbers.append(value) # .append() method: appends a new element; A method is a function belonging to an object. In this example, the list numbers.
print(numbers)
numbers.append(20)
numbers = [10, -2, 3, 99, 0]
index = numbers.index(-2)
print(index)
value = input("Enter a value: ")
index = numbers.index(value) # Be careful that value has a string, not integer.
print(index)
How to fix?numbers = [10, -2, 3, 99, 0] numbers.insert(2, 7) # 7 into numbers[2] print(numbers)
numbers = [10, -2, 3, 99, 0] numbers.insert(2, 99) numbers.remove(99) print(numbers) # What results?What if a number that is not in the list?
numbers = [10, -2, 3, 99, 0] numbers.sort() print(numbers) # What results? names = ["John", "Ruth", "Tom", "dog", "cat", "zebra"] names.sort() # Upper letters first print(names) names = ["John", "Ruth", "Tom", "dog", "cat", "zebra"] names.sort(key=str.lower) # In dictionary order; let's just use key=str.lower for a while. print(names) record = ["CS", 4, 3.9, True] record.sort() # Anything wrong here? print(record)
reverse is a keyword argumentnumbers = [10, -2, 3, 99, 0] numbers.sort(reverse=True) print(numbers) # What results?
numbers = [10, -2, 3, 99, 0] numbers.reverse() print(numbers) # What results?
numbers = [10, -2, 3, 99, 0] newNumbers = numbers[2:4] # numbers[1], ..., numbers[4-1] will be taken out. print(newNumbers) # What results?
msg = 'What a wonderful programming language, Python' newMsg = msg[5:16] # msg[5], ..., msg[16-1] will be taken out. print(newMsg) # What results?
range() are called sequence data types.
The above sub-sequence operation is supported.
in and not in operations are also supported.
for loops can be used with them.
name = 'Williams'
print('l' in name) # ???
print('l' not in name) # ???
print(name[2:5]) # ???
print(name[6]) # ???
print(len(name)) # ???
for c in name:
print(c)
student1 = ['John', 20, 'COMP'] student2 = ['Ruth', 19, 'MATH'] students = [student1, student2, ['Tom', 21, 'COMP']] print(students) print(students[0]) print((students[1])[2]) print(students[1][2])
students[1] has ['Ruth', 19, 'MATH'].
This is why (students[1])[2] has 'MATH'.
(students[i])[j] can be denoted as students[i][j].
sum = sum + 10 or sum = sum / 10?
Let's try the next example.
sum = 0
for i in range(5):
value = input('Enter a value: ')
value = int(value)
sum = sum + value
print(sum)
Let's try another example.
????
for i in range(5):
value = input("Enter a value: ")
value = int(value)
sum += value # +=: addition assignment operator
print(sum)
- * / % //???? sum += 5 sum -= 5 # sum = sum - 5 sum ??? 5 # sum = sum * 5 sum ??? 5 # sum = sum / 5 sum ??? 5 # sum = sum % 5 sum ??? 5 # sum = sum // 5
(...) is used instead of [...].
But [...] is used to access elements.
record = ('John', 20, 'COMP')
print(type(record)) # type() to check the data of a variable or value
print(record[1])
r = record[0:2]
print(r)
record[2] = 'MATH' # ???
number = int('345')
data_type(), are used to change the data type of a variable or value.
record = ('John', 20, 'COMP')
print(record)
recordList = list(record) # list()
print(recordList)
recordTuple = tuple(recordList) # tuple()
print(recordTuple)
size1 = 123 size2 = size1 print(size1) # ??? print(size2) # ??? size1 = 345 print(size1) # ??? print(size2) # ???
id() funtion - id for identity.
record1 = ['John', 20, 'COMP']
record2 = record1
print('record1 = ' + str(record1))
print('record2 = ' + str(record2))
print('The reference stored in record1 = ' + str(id(record1)))
print('The reference stored in record2 = ' + str(id(record2)))
del(record1[1])
record1.append('Interesting')
print('record1 = ' + str(record1))
print('record2 = ' + str(record2))
print('The reference stored in record1 = ' + str(id(record1)))
print('The reference stored in record2 = ' + str(id(record2)))
record1 = ('John', 20, 'COMP')
record2 = record1
print('record1 = ' + str(record1))
print('record2 = ' + str(record2))
print('The reference stored in record1 = ' + str(id(record1)))
print('The reference stored in record2 = ' + str(id(record2)))
del(record1) # The variable record1 that holds a reference value is deleted.
# But the tuple value is still referred from record2
#print('record1 = ' + str(record1)) # We cannot use record1 anymore.
print('record2 = ' + str(record2))
print('The reference stored in record2 = ' + str(id(record2)))
def add(size):
size += 2
num = 20
print(num)
add(num)
print(num) # 20 or 22 ???
def append(r):
r.append('Kamloops')
record = ['John', 20, 'COMP']
print(record)
append(record)
print(record) # ???
def increase(l, b): # increase all the values in l by b
for i in range(????): # how to get the length of a list?
l[i] ????
marks = []
for i in ???(5): # 5 integers
marks.append(int(input("Integer: ")))
print(marks)
increase(marks, 5)
print(marks) # ???
record1 = ['John', 20, 'COMP']
record2 = record1
print('record1 = ' + str(record1))
print('record2 = ' + str(record2))
print('The reference stored in record1 = ' + str(id(record1)))
print('The reference stored in record2 = ' + str(id(record2)))
import copy
record1 = ['John', 20, 'COMP', ['Kamloops', 'BC', 'Canada']]
record2 = copy.copy(record1) # copy the value, not the reference; but the refernce of ['Kamloops', 'BC', 'Canada'] is copied.
print('The reference stored in record1 = ' + str(id(record1)))
print('The reference stored in record2 = ' + str(id(record2))) # the same?
record1[0] = 'Tom'
record1[3][0] = 'Chase'
print('record1 = ' + str(record1))
print('record2 = ' + str(record2))
record2 = copy.deepcopy(record1) # copy the value, not the reference; even list elements
print('The reference stored in record1 = ' + str(id(record1)))
print('The reference stored in record2 = ' + str(id(record2))) # the same?
record1[3][0] = 'Kamloops'
print('record1 = ' + str(record1))
print('record2 = ' + str(record2))
[1, 2, [3, 4], 5]]?
The reference of [3, 4] will be copied with copy().
We may investigate the deepcopy() function to copy even elements.row = ['O', 'O', 'X'] will be printed as O|O|X.
from and to.
It makes a list of randomly scattered numbers from to to.
It returns the list.
row = [2, 0, 4] will be printed as |2| |4|.
'M', and all the other characters are ' '.
The position of 'M' should be randomly decided.
The function returns the list.
'M' is replaced by
'0' if there is no neighbor element having 'M', or'1' if there is one neighbor element having 'M', or '2' if two neighbor elements have 'M'.
['M',' ',' ',' ',' ','M',' ','M',' ',' '] becomes ['M','1','0','0','1','M','2','M','1','0'].
'|'.
E.g., |M|1|0|0|1|M|2|M|1|0| for board = ['M','1','0','0','1','M','2','M','1','0'].
Anoter example, | | |0| |1| | | | | | for board = [' ',' ','0',' ','1',' ',' ',' ',' ',' '].
(i, c),
where i for the position and c for the guess for '0', '1', '2', or 'M'.
' '.